package com.panda.pandawash.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.panda.pandawash.R;
import com.panda.pandawash.activity.MotorMonitorActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*创建一个 适配器设备列表 的类，继承BaseAdapter*/
public class AdapterBindDevicesList extends BaseAdapter {

	private Context mContext;
	private List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
	private LayoutInflater mInflater;

	public AdapterBindDevicesList(List<Map<String, Object>> data, Context mContext) {
		mInflater = LayoutInflater.from(mContext);
		this.data = data;
		this.mContext = mContext;
	}

	@Override
	public int getCount() {//返回数据长度
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	/*
	显示列表 
	*/
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder mHolder;	//创建一个ViewHolder用来快速的配置布局内容属性
		if (convertView == null) {
			mHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_bind_device, null);	//获取item_bind_device的布局文件
			convertView.setTag(mHolder);
		}
		else {
			mHolder = (ViewHolder) convertView.getTag();
		}

			/*获取控件句柄*/
			mHolder.bindDeviceName = (TextView) convertView.findViewById(R.id.bind_device_name);
			mHolder.bindStatus = (TextView) convertView.findViewById(R.id.bind_status);
			mHolder.bindItem = (LinearLayout) convertView.findViewById(R.id.bind_list_item);
			mHolder.bindImg = (ImageView) convertView.findViewById(R.id.bind_img);
			mHolder.bindDeviceName.setText(data.get(position).get(MotorMonitorActivity.EXTRAS_DEVICE_NAME).toString());
			
			if (data.get(position).get("near_by") == null || Integer.valueOf(data.get(position).get("near_by").toString()) == 0) {
				mHolder.bindStatus.setText("离线");
				mHolder.bindStatus.setTextColor(Color.parseColor("#E4E4E4"));
				mHolder.setContrast(0, mHolder.bindImg);
				//mHolder.bindItem.setBackgroundColor(Color.parseColor("#E4E4E4"));
			} else if (Integer.valueOf(data.get(position).get("near_by").toString()) == 1) {
				mHolder.bindStatus.setText("在线");
				mHolder.bindStatus.setTextColor(Color.parseColor("#000000"));
				mHolder.setContrast(1, mHolder.bindImg);
			} else {
				mHolder.bindStatus.setText("离线");
				mHolder.bindStatus.setTextColor(Color.parseColor("#E4E4E4"));
				//mHolder.bindItem.setBackgroundColor(Color.parseColor("#E4E4E4"));
				mHolder.setContrast(0, mHolder.bindImg);
			}

		return convertView;
	}

	public class ViewHolder {
		TextView bindDeviceName;
		TextView bindStatus;
		LinearLayout bindItem;
		ImageView bindImg;
		/*
		 * 在这里利用滤镜把图标设置成灰色或者彩色
		 * 用来表示设备是否离线
		 */
		public void setContrast(int contrast, ImageView img){
			ColorMatrix matrix = new ColorMatrix();	//新建颜色矩形对象
			matrix.setSaturation(contrast);	//设置矩形影响颜色饱和度
			ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);//创建一个矩形的彩色滤光片
			img.setColorFilter(filter); //设置图片滤镜效果
		}
		
	}

}

